package com.ws.shop.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.ws.shop.action.admin.BaseAdminAction;
/**  
 * 拦截器  
 */  
public class AdminLoginInterceptor extends HandlerInterceptorAdapter {
	private boolean auth = true;
	private String[] excludeUrls;

	private String loginUrl;
	private String processUrl;
	private String returnUrl;
	
	//预处理   
    public boolean preHandle(HttpServletRequest request,   
            HttpServletResponse response, Object handler) throws Exception {
    	String uri = request.getRequestURI();
    	if (exclude(uri)) {
			return true;
		}
        HttpSession session = request.getSession(false);
        if(BaseAdminAction.getLoginAdmin(session) == null){
        	response.sendRedirect(loginUrl);
            return false;   
        }   
        return true;   
    }   
  
//    public void postHandle(HttpServletRequest request,   
//            HttpServletResponse response, Object handler,   
//            ModelAndView modelAndView) throws Exception {   
//        
//    }   
//  
//    public void afterCompletion(HttpServletRequest request,   
//            HttpServletResponse response, Object handler, Exception ex)   
//            throws Exception {   
//    }
    
    private boolean exclude(String uri) {
		if (excludeUrls != null) {
			for (String exc : excludeUrls) {
				if (exc.equals(uri)) {
					return true;
				}
			}
		}
		return false;
	}
    
    public boolean isAuth() {
		return auth;
	}

	public void setAuth(boolean auth) {
		this.auth = auth;
	}

	public String[] getExcludeUrls() {
		return excludeUrls;
	}

	public void setExcludeUrls(String[] excludeUrls) {
		this.excludeUrls = excludeUrls;
	}

	public String getLoginUrl() {
		return loginUrl;
	}

	public void setLoginUrl(String loginUrl) {
		this.loginUrl = loginUrl;
	}

	public String getProcessUrl() {
		return processUrl;
	}

	public void setProcessUrl(String processUrl) {
		this.processUrl = processUrl;
	}

	public String getReturnUrl() {
		return returnUrl;
	}

	public void setReturnUrl(String returnUrl) {
		this.returnUrl = returnUrl;
	}
}  
